package com.dycn.controller;

import com.dycn.dao.RedisDao;
import com.dycn.model.*;
import com.dycn.service.ActivitysService;
import com.dycn.service.TaskService;
import com.dycn.util.AppResultData;
import com.dycn.util.CommGson;
import com.dycn.util.OkhttpUtil;
import com.dycn.util.UserConfigUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("tasks")
public class TaskController {
    @Autowired
    TaskService taskService;

    @Autowired
    RedisDao redisDao;

    @Autowired
    ActivitysService activitysService;

    @RequestMapping("list")
    public String list(Model model, String openid, @RequestParam(value = "type", defaultValue = "0") String type,Integer activityId, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String code) throws UnsupportedEncodingException {
        CyUser cyUser = getUser(httpServletRequest, httpServletResponse);

        if (cyUser == null && code == null&&!UserConfigUtil.isDebug) {
//           // return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaa974928147c87b9&redirect_uri=http%3a%2f%2fwx.sxbq998.cn/tasks/list&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//            model.addAttribute("activityId",activityId+"");
//            model.addAttribute("url",url);
            String url=OkhttpUtil.getInstance().getUrl(httpServletRequest);
            String realUrl=url+"tasks/list?type="+type+"&activityId="+activityId;
            model.addAttribute("url",URLEncoder.encode(realUrl,"utf-8"));
            if (type.equals("1"))
                return "dzhd/wexintoupiao";
            return "dzhd/wexin";
        }
        String typeString="tasks";
        if(type.equals("1")){
            typeString="dianzan";
        }
        //String startDate=redisDao.getValue(typeString+"startDate");
        //String endDate=redisDao.getValue(typeString+"endDate");
        List<TasksActivitiesVo> tasksActivitiesList = taskService.list(cyUser, code, type,activityId, httpServletResponse);
        ActivitysVo activitys=activitysService.selectByPrimaryKey(activityId).toActivitysVo();
        model.addAttribute("list", tasksActivitiesList);
       // model.addAttribute("startDate",activitys.getStartDate());
       // model.addAttribute("endDate",activitys.getEndDate());
        model.addAttribute("data",activitys);
        if (type.equals("1"))
            return "dzhd/toupiaos";
        else {
            return "dzhd/toupiao";
        }
    }

    @RequestMapping("detail")
    public String detail(Model model, String taskId,Integer activityId, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        CyUser user = getUser(httpServletRequest, httpServletResponse);
        TasksActivitiesVo tasksActivities = taskService.findById(user.getOpenid(), taskId);
        Activitys activitys=activitysService.selectByPrimaryKey(activityId);
        model.addAttribute("act",activitys);
        model.addAttribute("data", tasksActivities);
        return "dzhd/detail";
    }


    @RequestMapping("modify")
    public String modify(Model model, String id, String type) {
        TasksActivities tasksActivities = taskService.findId(id);
        model.addAttribute("data", tasksActivities);
        if (type.equals("0"))
            return "dzhd/updz";
        else
            return "dzhd/uptoupiao";
    }

    @PostMapping("saveOrUpdate.json")
    @ResponseBody
    public AppResultData saveOrUpdate(TasksActivities tasksActivities) {
        String user = SecurityUtils.getSubject().getPrincipal().toString();
        return taskService.saveOrUpdate(user, tasksActivities);
    }

    @PostMapping("dianzan.json")
    @ResponseBody
    public AppResultData dianzan(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String taskId, Integer type,Integer activityId) {

        CyUser user = getUser(httpServletRequest, httpServletResponse);
        if (user == null) {
            return AppResultData.errorMessage("请在微信端打开");
        }
        return taskService.dianzan(user, taskId, type,activityId);
    }

    @PostMapping("zhuli.json")
    @ResponseBody
    public AppResultData zhuli(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String taskId,Integer activityId) {

        CyUser user = getUser(httpServletRequest, httpServletResponse);
        if (user == null) {
            return AppResultData.errorMessage("请在微信端打开");
        }
        return taskService.zhuli(user, taskId,activityId);
    }

    @PostMapping("toupiao.json")
    @ResponseBody
    public AppResultData toupiao(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String taskId) {

        CyUser user = getUser(httpServletRequest, httpServletResponse);
        if (user == null) {
            return AppResultData.errorMessage("请在微信端打开");
        }
        return taskService.toupiao(user, taskId);
    }

    @PostMapping("delete.json")
    @ResponseBody
    public AppResultData delete(String id) {
        return AppResultData.success("成功", taskService.deleteById(id));
    }

    private CyUser getUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Cookie[] cookies = httpServletRequest.getCookies();
        CyUser user = null;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("user")) {
                    String userString = redisDao.getValue(cookie.getValue());
                    cookie.setMaxAge(30 * 60 * 1000);
                    cookie.setPath("/");
                    httpServletResponse.addCookie(cookie);
                    redisDao.putSecondValue("user", cookie.getValue(), 30 * 60 * 1000);
                    user = CommGson.getInstance().toObject(userString, CyUser.class);
                }
            }
        }
        return user;
    }
}
